//统计一个数字在排序(顺序)中出现的次数
#include<stdio.h>
int BinarySearch(int* nums, int numsSize, int target, int flag) {
	int left, right, mid, index;
	index = numsSize;
	left = 0;
	right = numsSize - 1;
	while (left <= right ) {
		//mid = (left + right) / 2;
		mid = left + ((right - left) >> 1);
		if (nums[mid] > target || (nums[mid] >= target && flag)) {
			right = mid - 1;
			index = mid;
		}
		else
			left = mid + 1;
	}
	return index;
}

int search(int* nums, int numsSize, int target) {
	int left = BinarySearch(nums, numsSize, target, 1);//第一个等于target的元素下标
	int right = BinarySearch(nums, numsSize, target, 0);//第一个大于target的元素下标
	//不存在时返回numsSize
	int ret = 0;
	if (left != numsSize)
		ret = right - left;
	return ret;
}

int main() {
	int nums[] = { 1,2,3,4,5,5,5,6 };
	int numSize = sizeof(nums) / sizeof(nums[0]);
	int target;
	printf("输入要统计的数字>>");
	scanf("%d", &target);
	int ret = search(nums, numSize, target);
	printf("%d 出现的次数: %d\n", target, ret);
	return 0;
}